package in.muton.edushare.schedules;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.List;

import com.edushare.common.dao.ScheduleDAO;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.ScheduleNotifications;
import com.edushare.common.vo.Schedules;
import com.edushare.common.vo.Users;

public class NewSchedule {
	public NewSchedule(Schedules sch, Object obj, int type, int id) {
		this(sch,obj,type,id, new ArrayList<Users>());
	}
	
	public NewSchedule(Schedules sch, Object obj, int type, int id, List<Users> userList){
		this.sch = sch;
		cType = type;
		cId = id;
		this.o = obj;
		this.u = userList;
	}

	public NewSchedule(Schedules sch, List<Users> userList, Users creator) {
		this.u = userList;
		this.sch = sch;
		this.o = creator;
		cType = Messages.RECEIVER_TYPE_USER;
		cId = creator.getUserId();
	}

	public void send() {
		sch.setCreatorType(cType);
		sch.setSchCreator(cId);
		ScheduleDAO sd = new ScheduleDAO(EduShareSession.get()
				.getSessionORMInterface());
		sd.addToSchedules(sch);
		
		List<Users> uList = new ArrayList<Users>();
		uList.addAll(u);
		Users creator = sch.getCreatingUser();
		uList.remove(creator);
		sd.sendNotifications(sch, new CayenneList<Users>(uList)
							.getObject(), ScheduleNotifications.INVITED);
		sd.sendNotifications(sch, creator, ScheduleNotifications.OPEN);
	}

	int cType = 0;
	int cId = 0;
	List<Users> u = null;
	Schedules sch = null;
	Object o = null;
}
